package com.hnhcyy.goods.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hnhcyy.goods.pojo.Banner;
import com.hnhcyy.goods.pojo.Goods;
import com.hnhcyy.goods.pojo.GoodsPre;
import com.hnhcyy.goods.pojo.GoodsQueryParam;
import com.hnhcyy.goods.pojo.SpitemRelation;
import com.hnhcyy.goods.pojo.SpkfkDesc;
import com.hnhcyy.goods.service.BannerService;
import com.hnhcyy.goods.service.GoodsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value="/GoodsApi", tags= "商品接口")
@RestController
@RequestMapping("/goods")
public class GoodsApi {

	@Autowired 
	private GoodsService goodsService;
	@Autowired 
	private BannerService bannerServcie;

	/**
	 * 获取商品列表
	 * @author sheyongjun
	 * @date 2019年1月15日
	 */
	@ApiOperation(value = "商品列表", notes = "根据条件获取商品列表")
	@GetMapping("/list")
	public List<Goods> goodsList(GoodsQueryParam queryParam) {
		System.out.println("=====offset======" + queryParam.getOffset());
		return goodsService.getListByQueryParam(queryParam);
	}

	/**
	 * 根据ID获取商品属性-最必要的字段
	 * @author sheyongjun
	 * @date 2019年2月15日
	 */
	@ApiOperation(value = "商品基本属性", notes = "根据ID获取商品最必要的字段")
	@ApiImplicitParam(name = "spid", value = "商品ID", required = false, dataType = "String",paramType = "query")
	@GetMapping("/info/little")
	public Goods goodsInfoLittle(String spid) {
		return goodsService.getItemById(spid);
	}
	
	/**
	 * 根据ID获取商品基本信息
	 * @author sheyongjun
	 * @date 2019年1月17日
	 */
	@ApiOperation(value = "商品基本信息", notes = "根据ID获取商品基本信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "spid", value = "商品ID（不用传dwbh和appointGoods）", required = false, dataType = "String",paramType = "query"),
		@ApiImplicitParam(name = "sptm", value = "商品条码（必传dwbh和appointGoods）", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "dwbh", value = "单位ID", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "sjid", value = "商家ID", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "appointGoods", value = "是否指定商品（默认false）", required = false, dataType = "boolean", paramType = "query"),
	})
	@GetMapping("/info/more")
	public Goods goodsInfo(String spid, String sptm, @RequestParam(defaultValue = "1") int sjid, HttpServletRequest request) {
		if(StringUtils.isNotBlank(spid)) {
			// 通过spid获取详情，用户一定是登录后操作的，故无需校验商品是否可销。
			return goodsService.getItemById(spid, null, sjid);
		}
		if(StringUtils.isNotBlank(sptm)) {
			// 商品条码查询
			String dwbh = request.getParameter("dwbh");
			String appointGoods = request.getParameter("appointGoods");
			if(StringUtils.isBlank(dwbh)  || StringUtils.isBlank(appointGoods)) {
				// dwbh、appointGoods三者缺一不可
				return null;
			}
			Goods goods = goodsService.getItemById(null, sptm, sjid);
			if(goods == null) {
				return null;
			}
			// 检查该商品对于该客户是否可销
			GoodsQueryParam queryParam = new GoodsQueryParam(goods.getSpid(), dwbh, sjid, Boolean.parseBoolean(appointGoods));
			boolean isSell = goodsService.checkSell(queryParam);
			if(!isSell) {
				return null;
			}
			return goods;
		}
		// 如果spid和sptm都为空，则返回null
		return null;
	}

	/**
	 * 商品图文描述
	 * @author sheyongjun
	 * @date 2019年1月18日
	 */
	@ApiOperation(value = "商品图文描述", notes = "根据ID获取商品图文描述")
	@ApiImplicitParam(name = "spid", value = "商品ID", required = true, dataType = "String", paramType = "query")
	@GetMapping("/desc")
	public SpkfkDesc goodsDesc(String spid) {
		return goodsService.getDescById(spid);
	}

	/**
	 * 关联商品
	 * @author sheyongjun
	 * @date 2019年1月18日
	 */
	@ApiOperation(value = "关联商品", notes = "根据商品ID获取该商品的关联商品")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "sjid", value = "商家ID", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "dwbh", value = "客户ID", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "appointGoods", value = "是否指定品种", required = true, dataType = "boolean", paramType = "query"),
		@ApiImplicitParam(name = "spid", value = "主商品ID", required = true, dataType = "String", paramType = "query"),
	})
	@GetMapping("/relation")
	public List<SpitemRelation> goodsRelation(GoodsQueryParam queryParam) {
		return goodsService.getRelationById(queryParam);
	}
	
	/**
	 * 即将到货商品
	 * @author sheyongjun
	 * @date 2019年1月22日
	 */
	@ApiOperation(value = "即将到货的商品", notes = "即将到货的商品")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "sjid", value = "商家ID", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "page", value = "页码", required = false, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "size", value = "每页显示条数", required = false, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "search", value = "搜索", required = false, dataType = "String", paramType = "query"),
	})
	@GetMapping("/prelist")
	public List<GoodsPre> prelist(@RequestParam(defaultValue = "1") int sjid, 
								@RequestParam(defaultValue = "1") int page, 
								@RequestParam(defaultValue = "10") int size,
								String search) {
		return goodsService.getPreList(sjid, search, page, size);
	}
	
	/**
	 * 活动商品
	 * @author sheyongjun
	 * @date 2019年2月13日
	 */
	@ApiOperation(value = "促销商品", notes = "有促销活动的商品（活动专区）")
	@GetMapping("/promotionlist")
	public List<Goods> promotionList(GoodsQueryParam queryParam){
		return goodsService.getPromotionList(queryParam);
	};

	/**
	 * 检查一个商品对于客户是否可销
	 * @author sheyongjun
	 * @date 2019年1月17日
	 */
	@ApiOperation(value = "商品-客户是否可销", notes = "检查一个商品对于客户是否可销")
	@PostMapping("sellcheck")
	public boolean sellcheck(@RequestBody GoodsQueryParam queryParam) {
		return goodsService.checkSell(queryParam);
	}

	/**
	 * banner图
	 * @author sheyongjun
	 * @date 2019年1月19日
	 */
	@ApiOperation(value = "轮播图", notes = "主页轮播图")
	@GetMapping("bannerlist")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "devicetype", value = "设备类型（web/mobile）", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "sjid", value = "商家ID", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "dwbh", value = "客户ID", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "appointGoods", value = "是否指定品种", required = false, dataType = "boolean", paramType = "query"),
	})
	public List<Banner> bannerlist(@RequestParam(defaultValue="web") String devicetype, 
									@RequestParam(defaultValue="1") int sjid, 
									@RequestParam(defaultValue="false") boolean appointGoods,
									String dwbh){	
		return bannerServcie.getList(devicetype, sjid, dwbh, appointGoods);
	}

}
